// hand.cpp – Implementation of the class Hand
#include <iostream>
#include <string>
#include “hand.h”
#include “card.h” 
using namespace std; 

// Default constructor
Hand::Hand() {
	cards = new Card[5];
	numCards = 0;
}

// Copy constructor 
Hand::Hand(Hand& h) {
	numCards = h.numCards;
	cards = new Card[5];	
	for (int i = 0; I < numCards; i++) {
		cards [i] = h.cards[i]; // remember that is a member-wise copy.
	}
}

// Destructor
Hand::~Hand() {
	delete [] cards;
}

// Returns number of cards
int Hand::getNumOfCards() const {
	return numCards;
}

// Adds a card to the hand
void Hand::addCard(Card& c) {
	if (numCards == 5) {
		cout << “Hand is full” << endl;
      }
	else {
		cards[numCards] = c;
		numCards++;
	}
}

// Deletes a card by specifying the number and suit
void Hand::removeCard(int pos) {
	if ((pos <= 0) && (pos >= numCards)) {
		cout << “Invalid position” << endl;
		return;	
	}
	
	numCards--; 	// update numCards
	
	// We shift the elements forward
	for (int i = pos; i < numCards; i++) {
		cards[i] = cards[i+1];
	}

	// You can also check that the number of cards is minimum 2 but this is not
	// mandatory
}

// Returns a Card that is stored at position pos
Card& Hand::get(int pos) const {
	if ((pos <=0) || (pos >= numCards)) {
     	 cout << “Invalid position “ << endl;
		 // Instead of returning a fake card, we will return the first element 
 		 // of the array in case the position is invalid.
		 // If you did not do that, you will not lose marks.
		 cout << “First element returned “ << endl;
		 return cards[0];
     }
	return cards[pos];
} 

// Returns true if a given card belongs to this hand
bool Hand::search(int value, string suit) { 
	for (int i = 0; i < numCards; i++) {
		 if ((cards[i].getValue() == value) && (cards[i].getSuit() == suit))
			return true;
	}
	return false;
}

// Returns true if two objects of Hand contain the same cards
bool Hand::sameHands(Hand& h) {
	for (int i = 0; i < numCards; i++) {
		// You can also use a double loop instead of the function search
        if (h.search(cards[i].getValue(), cards[i].getSuit()) == false)
		   return false; 
	}
	return true;
}

// Prints the content of a hand
void Hand::print() const {
	for (int i = 0; i < numCards; i++) {
		cards[i].print();
	} 
}
